package quiz;


import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;

import org.junit.Test;

public class DataBaseTest {

	public DataBase database = new DataBase();

	@Test
	public void Basic() {
		assertTrue(!database.isOpen());
		database.close();
		assertTrue(!database.isOpen());
		database.open();
		assertTrue(database.isOpen());
		database.close();
		assertTrue(!database.isOpen());
		database.open();
		
		System.out.println("B");
		database.parseXML("bunny.xml");
		database.parseXML("cities.xml");
	}

	public void AnswerTest() {
		Answer.setDataBase(database);
		Answer answer = new Answer(1, "what what in the butt", Answer.TEXT, true,2);
		database.storeAnswer(answer);

		Answer compare = database.getAnswer(answer.getID());
		assertTrue(answer.equals(compare));
		Answer answer2 = new Answer(1, "holy shitting god, yes", Answer.TEXT, true, 3);
		database.storeAnswer(answer2);
		Answer compare2 = database.getAnswer(answer2.getID());
		assertTrue(answer2.equals(compare2));
	}

	public void QuestionTest() { //doesn't check edits or delete
		Question.setDataBase(database);
		Question question = new Question(1, "Is SQL fucking stupid?", Question.MC, 100, 1, 5);
		database.storeQuestion(question);
		Question compare = database.getQuestion(question.getID());
		assertTrue(question.equals(compare));
		Question question2 = new Question(1, "Anger is hard to communicate.", Question.FITB, 500, 30, 2);
		database.storeQuestion(question2);
		Question compare2 = database.getQuestion(question2.getID());
		assertTrue(question2.equals(compare2));
	}

	@Test
	public void AccountTest() {
		Account.setDataBase(database);
		Account account = new Account("Fook Mi", "Fmi", Account.hashPassword("nishantisabitch"), false);
		database.storeAccount(account);
		Account compare = database.getAccount(account.getID());
		assertTrue(account.equals(compare));
		Account account2 = new Account("Fook Yu", "Fyu", Account.hashPassword("nishantisaslut"), true);
		database.storeAccount(account2);
		Account compare2 = database.getAccount(account2.getID());
		assertTrue(account2.equals(compare2));
		//TODO: check addfriends and getfriends
	}

	@Test
	public void QuizTest() { //doesn't check edits or delete
		Quiz.setDataBase(database);
		System.out.println("d");
		Quiz quiz = new Quiz("How to give 101 shitfucks", "this is stupid", true, true, true, true, true, 1);
		System.out.println("d");
		database.storeQuiz(quiz);
		System.out.println("B");
		Quiz compare = database.getQuiz(quiz.getID());
		assertTrue(quiz.equals(compare));
		Quiz quiz2 = new Quiz("Figure out if you are an asshat", "this is stupid", false, false, false, false, false, 1);
		database.storeQuiz(quiz2);
		Quiz compare2 = database.getQuiz(quiz2.getID());
		assertTrue(quiz2.equals(compare2));
	}

	public void achievementsTest() {
		Achievement.setDataBase(database);
		Achievement achievement = new Achievement(69,69);
		database.storeAchievement(achievement);
		ArrayList<Achievement> compare = database.getUserAchievements(achievement.getUserID());
		assertTrue(achievement.equals(compare.get(0)));
		assertEquals(compare.size(), 1);
	}

	public void resultsTest() {
		Result.setDataBase(database);
		Result result = new Result(1,1,10,5,false);
		database.storeResult(result);
		Result compare = database.getResult(result.getID());
		assertTrue(result.equals(compare));

		Result result2 = new Result(1,1,15,5,false);
		Result result3 = new Result(1,1,13,5,false);
		Result result4 = new Result(1,1,12,5,false);
		Result result5 = new Result(1,1,11,5,false);
		database.storeResult(result2);
		database.storeResult(result3);
		database.storeResult(result4);
		database.storeResult(result5);
		ArrayList<Result> recentForUser = database.getRecentResultsForUser(1, 10);
		ArrayList<Result> globalRecent = database.getGlobalRecentResults(5);
		ArrayList<Result> quizTop = database.getTopQuizResults(1, 5);

		assertTrue(recentForUser.size() == globalRecent.size());
		assertTrue(globalRecent.size() == quizTop.size());
		assertTrue(recentForUser.size() == 5);

	}

	public void deleteTest() {
		Account.setDataBase(database);
		Account act1 = database.getAccount(1);
		Account act2 = database.getAccount(3);
		database.deleteAccount(act1);
		database.deleteAccount(act2);
	}

	//TODO:have to test the deletes, edits and get top stuff

}
